﻿using Autofac;
using FluentValidation;
using Lamp.Core;
using Lamp.Core.Common.Logger;
using Lamp.Core.Server;
using Lamp.Server;

namespace Lamp.Validation
{
    public static partial class ServiceHostBuilderExtension
    {
        public static IServiceHostServerBuilder UseValidation(this IServiceHostServerBuilder serviceHostBuilder)
        {
            serviceHostBuilder.RegisterService(containerBuilder =>
            {
                containerBuilder.RegisterType<ValidatorFactory>().As<IValidatorFactory>().SingleInstance();
            });

            serviceHostBuilder.AddInitializer(container =>
            {
                ILogger logger = container.Resolve<ILogger>();
                logger.Info($"[config]use Validation module");

                IServer server = container.Resolve<IServer>();
                server.UseMiddleware<ValidMiddleware>(container);
            });

            return serviceHostBuilder;
        }
    }
}
